<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>select 组件</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #input{
            position: absolute;
            top: 100px;
            left: 100px;
            width: 250px;
            height: 32px;
        }
        ul{
            position: absolute;
            list-style: none;
            background-color: #fff;
            border: 1px solid #ccc;
            display: none;
        }
        ul li{
            padding: 10px;
            width: 100px;
            border-bottom: 1px solid #ccc;
        }
        ul li:hover{
            background-color: #eee;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <input type="text" name="" id="input" autocomplete="off" >
    <ul id="select">
        <li>北京</li>
        <li>上海</li>
        <li>广州</li>
        <li>深圳</li>
    </ul>
    <script>
        window.onload = function() {
            var input = document.getElementById('input');
            var selectEml = document.getElementById('select')
            ;(function() {
                window.select = function(eml) {
                    eml.addEventListener('focus', function(e) {
                        selectEml.style.display = 'block'
                        let ops = this.getBoundingClientRect()
                        
                        selectEml.style.top = ops.y + this.offsetHeight + 'px'
                        selectEml.style.left = ops.x + 'px'
                    })
                    eml.addEventListener('click', function(e) {
                        e.stopPropagation();
                    })
                    document.addEventListener('click', function() {
                        selectEml.style.display = 'none'
                    })
                }
            })();

            select(input);
        }
    </script>
</body>
</html>